package zeronet

import "log"

type AdminZite struct {
	*zeroSocket
	sizeObj map[string]map[string]interface{}
}

func NewAdminZite(zeroSocket *zeroSocket) *AdminZite {
	a := &AdminZite{zeroSocket: zeroSocket, sizeObj: make(map[string]map[string]interface{})}
	return a
}

//获取所有site
func (self *AdminZite) ReloadSiteList() map[string]map[string]interface{} {
	log.Println("Reloading sites list")
	m := &Msg{
		Cmd: "siteList",
	}

	msg := self.Send(m)
	sites, ok := msg.Result.([]interface{})
	if ok {
		for _, v := range sites {
			site, ok := v.(map[string]interface{})
			if ok {
				addr := site["address"].(string)
				self.sizeObj[addr] = site
			}
		}
	}
	return self.sizeObj
}

//更新所有site
func (self *AdminZite) UpdateAll() {
	log.Println("Updating all sites")
	msg := self.ReloadSiteList()
	m := &Msg{
		Cmd: "siteUpdate",
	}
	for addr, _ := range msg {
		m.Params = addr
		self.Send(m)
		log.Println("Update sites: ", addr)
	}
}

//添加site
func (self *AdminZite) AddSites(addrs []string) {
	for _, addr := range addrs {
		m := &Msg{
			Cmd:    "siteAdd",
			Params: addr,
		}
		self.Send(m)
	}
}
